<template>
  <div class="content">
    <el-form ref="form" :model="form" label-width="80px" :inline="true">
      
      <el-form-item>
        <el-input clearable v-model="form.name" size="mini" placeholder="请输入食堂名称"></el-input>
      </el-form-item>

      <el-form-item>
        <el-select clearable v-model="form.deptId" placeholder="请选择所属学校" size="mini">
          <el-option :label="item.name" :value="item.id" v-for="item in schoolList" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-button type="primary" size="mini" class="btn" @click="search">
        <i class="fa fa-search" style="margin-right:5px;"></i>查询
      </el-button>
      <el-button
        type="primary"
        @click="editAddOrUpdate(1)"
        size="mini"
        class="btn"
        style="margin-right: 15px"
      >
        <i class="fa fa-plus" style="margin-right:5px;"></i>新增
      </el-button>
    </el-form>

    <!-- <el-divider></el-divider> -->

    <template>
      <el-table
        :data="tableData"
        height="470"
        v-loading="loading"
        size="mini"
        style="width: 100%"
      >
        <el-table-column prop="id" label="食堂ID" width="150"></el-table-column>
        <el-table-column prop="name" width="130" label="食堂名称"></el-table-column>

        <el-table-column prop="cadre" label="食堂责任人"></el-table-column>

        <el-table-column prop="cadrePhone" width="130" label="食堂责任人电话"></el-table-column>

        <el-table-column prop="phone" width="130" label="食堂电话"></el-table-column>

        <el-table-column prop="address" width="130" label="食堂地址"></el-table-column>

        <el-table-column prop="name" width="200" label="营业时间">
          <template slot-scope="scope">
            <button class="table_btn">早上 {{scope.row.breakfastTime}}</button>
            <button class="table_btn">中午 {{scope.row.lunchTime}}</button>
            <button class="table_btn" style="margin-bottom: 0px">晚上 {{scope.row.dinnerTime}}</button>
          </template>
        </el-table-column>
        <el-table-column prop="name" width="130" label="所属学校"></el-table-column>

        <el-table-column fixed="right" :label="$t('action.operation')" width="210">
          <template slot-scope="scope">
            <el-button size="mini" @click="editAddOrUpdate(2,scope.row.id)">
              <i class="fa fa-edit" style="margin-right:5px;"></i>
              {{$t('action.edit')}}
            </el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">
              <i class="fa fa-trash" style="margin-right:5px;"></i>
              {{$t('action.delete')}}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>

    <!--    分页功能-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[10, 15, 20,50,100]"
      :page-size="everyPageNum"
      layout="total, sizes,prev, pager, next, jumper"
      :total="total"
    ></el-pagination>

    <!--新增编辑弹框-->
    <el-dialog :title="updateStatus==1?'创建食堂':'编辑食堂'" :visible.sync="showEdit" width="60%" v-dialogDrag>
      <CanteenEdit @hideEdit="hideEditStatus" ref="child" :msg="updateStatus"></CanteenEdit>
    </el-dialog>
  </div>
</template>

<script>
import CanteenEdit from "@/components/CanteenManagement/edit";
import Cookies from "js-cookie";
export default {
  name: "editCanteen",
  components: {
    CanteenEdit
  },
  data() {
    return {
      url: this.$Config.url,
      loading: false,
      updateStatus: 1, //默认是新增
      everyPageNum: 10, //每页多少条
      currentPage: 1, //当前页
      showEdit: false, //显示编辑弹框
      schoolList: [], //学校数据
      form: {
        name: "",
        deptId: ""
      },
      tableData: [],
      total: 1
    };
  },
  mounted() {
    this.common();
    this.getSchool();
  },
  methods: {
    search() {
      this.common();
    },
    getSchool() {
      const that = this;
      this.$axios
        .get(that.url + "/admin/dept/list", {
          headers: {
            "x-baie-admin-token": Cookies.get("token")
          }
        })
        .then(function(res) {
          if (res.data.code == 200) {
            that.schoolList = res.data.data;
          } else {
            that.$message({
              message: res.data.msg,
              type: "warning"
            });
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    handleDelete(index, row) {
      this.$confirm("您确认要删除该食堂?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.deleteStudent(row.id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    deleteStudent(id) {
      const that = this;
      this.$axios
        .post(
          that.url + "/admin/market/delete/{id}",
          that.$qs.stringify({ id }),
          {
            headers: {
              "x-baie-admin-token": Cookies.get("token")
            }
          }
        )
        .then(function(res) {
          if (res.data.code == 200) {
            that.$message({
              type: "success",
              message: "删除成功!"
            });
            that.common(that.currentPage, that.everyPageNum);
          } else {
            that.$message({
              message: res.data.msg,
              type: "warning"
            });
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    common(pageNo = 1, pageSize = 10) {
      const that = this;
      this.loading = true;
      this.$axios
        .get(that.url + "/admin/market/page", {
          params: {
            pageNo,
            pageSize,
            name: that.form.name,
            deptId: that.form.deptId
          },
          headers: {
            "x-baie-admin-token": Cookies.get("token")
          }
        })
        .then(function(res) {
          that.loading = false;
          if (res.data.code == 200) {
            that.tableData = res.data.data.records;
            that.total = res.data.data.total;
          } else {
            that.$message({
              message: res.data.msg,
              type: "warning"
            });
          }
        })
        .catch(function(error) {
          that.loading = false;
          console.log(error);
        });
    },
    handleClick(row) {
      console.log(row);
    },
    handleSizeChange(val) {
      //   每页多少条
      this.everyPageNum = val;
      this.common(this.currentPage, val);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.common(val, this.everyPageNum);
    },
    editAddOrUpdate(status, id) {
      //新增或编辑
      /**
       * 1是新增
       * 2是编辑
       */
      this.showEdit = true;
      if (status == 1) {
        //新增
        this.updateStatus = 1;
        setTimeout(() => {
          this.$refs["child"].resetForm("ruleForm");
        }, 0);
      } else {
        //编辑
        this.updateStatus = 2;
        setTimeout(() => {
          this.$refs["child"].resetData(id);
        }, 0);
      }
    },
    hideEditStatus(status) {
      this.showEdit = false;
      this.common(this.currentPage, this.everyPageNum);
    }
  }
};
</script>

<style scoped lang="scss">
.el-pagination {
  float: right;
  margin: 20px 10px;
}
.table_btn {
  background: #eee;
  color: #7b9dda;
  border: none;
  outline: none;
  padding: 4px 10px;
  margin-bottom: 5px;
  border-radius: 5px;
}
.button_list {
  margin-bottom: 10px;
}

.content {
  text-align: left;
}

.el-divider {
  margin: 10px 0;
}

.btn {
  margin-top: 7px;
}

.el-form-item {
  margin-bottom: 0;
}
</style>
